WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
solver_ceres.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// Ceres includes
22#include "ceres/jet.h"
23#include "ceres/ceres.h"
24#include "glog/logging.h"
25
26// wolf includes
28#include "yaml-cpp/yaml.h"
29
30namespace ceres
31{
32typedef std::shared_ptr<CostFunction> CostFunctionPtr;
33}
34
35namespace wolf
36{
39
41{
42 protected:
43 std::map<FactorBasePtr, ceres::ResidualBlockId> fac_2_residual_idx_;
44 std::map<FactorBasePtr, ceres::CostFunctionPtr> fac_2_costfunction_;
45
46 // this map is only for handling automatic destruction of ManifoldWrapper objects
47 std::map<StateBlockPtr, ManifoldWrapperPtr> state_blocks_manifold_;
48
49 ceres::Solver::Summary summary_;
50 std::unique_ptr<ceres::Problem> ceres_problem_;
51 std::unique_ptr<ceres::Covariance> covariance_;
52
53 // profiling
54 unsigned int n_iter_acc_, n_iter_max_;
56
57 public:
58 SolverCeres(const ProblemPtr& _wolf_problem, const YAML::Node& _params);
59
61
62 ~SolverCeres() override;
63
64 ceres::Solver::Summary getSummary() const;
65
66 std::unique_ptr<ceres::Problem>& getCeresProblem();
67
70
71 bool computeCovariancesDerived(const std::vector<StateBlockPtr>& st_list) override;
72
73 bool converged() const override;
74 bool failed() const override;
75 bool wasStopped() const override;
76 unsigned int iterations() const override;
77 double initialCost() const override;
78 double finalCost() const override;
79 double totalTime() const override;
80
81 ceres::Solver::Options& getSolverOptions();
82
84
85 virtual int numStateBlocksEstimatedDerived() const override;
86
87 virtual int numFactorsDerived() const override;
88 virtual int sizeStateBlocksEstimatedDerived() const override;
89 virtual int sizeFactorsDerived() const override;
90
91 protected:
92 bool checkDerived(std::string prefix = "") const override;
93
94 std::string solveDerived(const ReportVerbosity report_level) override;
95
96 void addFactorDerived(const FactorBasePtr& fac_ptr) override;
97
98 void removeFactorDerived(const FactorBasePtr& fac_ptr) override;
99
100 void addStateBlockDerived(const StateBlockPtr& state_ptr) override;
101
102 void removeStateBlockDerived(const StateBlockPtr& state_ptr) override;
103
104 void updateStateBlockStatusDerived(const StateBlockPtr& state_ptr) override;
105
106 void updateStateBlockLocalParametrizationDerived(const StateBlockPtr& state_ptr) override;
107
108 ceres::CostFunctionPtr createCostFunction(const FactorBasePtr& _fac_ptr);
109
110 bool isFactorRegisteredDerived(const FactorBasePtr& fac_ptr) const override;
111
112 bool isStateBlockRegisteredDerived(const StateBlockPtr& state_ptr) const override;
113
114 bool isStateBlockFixedDerived(const StateBlockPtr& st) const override;
115
116 bool hasThisLocalParametrizationDerived(const StateBlockPtr& st,
117 const LocalParametrizationBasePtr& local_param) const override;
118
119 bool hasLocalParametrizationDerived(const StateBlockPtr& st) const override;
120
121 void printProfilingDerived(std::ostream& _stream) const override;
122
123 // PARAMS
124 ceres::Solver::Options solver_options_;
125};
126
127inline ceres::Solver::Summary SolverCeres::getSummary() const
128{
129 return summary_;
130}
131
132inline std::unique_ptr<ceres::Problem>& SolverCeres::getCeresProblem()
133{
134 return ceres_problem_;
135}
136
137inline ceres::Solver::Options& SolverCeres::getSolverOptions()
138{
139 return solver_options_;
140}
141
142inline bool SolverCeres::isFactorRegisteredDerived(const FactorBasePtr& fac_ptr) const
143{
144 return fac_2_residual_idx_.count(fac_ptr) == 1 and fac_2_costfunction_.count(fac_ptr) == 1;
145}
146
147inline bool SolverCeres::isStateBlockRegisteredDerived(const StateBlockPtr& state_ptr) const
148{
149 if (state_blocks_.count(state_ptr) == 0) return false;
150 return ceres_problem_->HasParameterBlock(getAssociatedMemBlockPtr(state_ptr));
151}
152
153inline bool SolverCeres::isStateBlockFixedDerived(const StateBlockPtr& st) const
154{
155 if (state_blocks_.count(st) == 0) return false;
156 return ceres_problem_->IsParameterBlockConstant(getAssociatedMemBlockPtr(st));
157};
158
159inline bool SolverCeres::hasLocalParametrizationDerived(const StateBlockPtr& st) const
160{
161 return state_blocks_manifold_.count(st) == 1;
162};
163
165{
166 return ceres_problem_->NumResidualBlocks();
167};
168
170{
171 return ceres_problem_->NumResiduals();
172}
173
174} // namespace wolf
Definition: manifold_wrapper.h:34
Definition: solver_ceres.h:41
bool isFactorRegisteredDerived(const FactorBasePtr &fac_ptr) const override
Definition: solver_ceres.h:142
bool hasThisLocalParametrizationDerived(const StateBlockPtr &st, const LocalParametrizationBasePtr &local_param) const override
Definition: solver_ceres.cpp:916
SolverCeres(const ProblemPtr &_wolf_problem, const YAML::Node &_params)
Definition: solver_ceres.cpp:30
virtual int numFactorsDerived() const override
Definition: solver_ceres.h:164
bool failed() const override
Definition: solver_ceres.cpp:688
ceres::Solver::Summary getSummary() const
Definition: solver_ceres.h:127
virtual int sizeFactorsDerived() const override
Definition: solver_ceres.h:169
ceres::Solver::Options solver_options_
Definition: solver_ceres.h:124
bool wasStopped() const override
Definition: solver_ceres.cpp:683
unsigned int n_no_convergence_
Definition: solver_ceres.h:55
bool isStateBlockFixedDerived(const StateBlockPtr &st) const override
Definition: solver_ceres.h:153
unsigned int n_interrupted_
Definition: solver_ceres.h:55
bool converged() const override
Definition: solver_ceres.cpp:678
void printProfilingDerived(std::ostream &_stream) const override
Definition: solver_ceres.cpp:924
double initialCost() const override
Definition: solver_ceres.cpp:699
virtual int sizeStateBlocksEstimatedDerived() const override
Definition: solver_ceres.cpp:668
bool isStateBlockRegisteredDerived(const StateBlockPtr &state_ptr) const override
Definition: solver_ceres.h:147
unsigned int n_iter_max_
Definition: solver_ceres.h:54
unsigned int n_iter_acc_
Definition: solver_ceres.h:54
bool checkDerived(std::string prefix="") const override
Definition: solver_ceres.cpp:730
void removeFactorDerived(const FactorBasePtr &fac_ptr) override
Definition: solver_ceres.cpp:565
~SolverCeres() override
Definition: solver_ceres.cpp:104
std::map< StateBlockPtr, ManifoldWrapperPtr > state_blocks_manifold_
Definition: solver_ceres.h:47
bool computeCovariancesDerived(CovarianceBlocksToBeComputed _blocks=CovarianceBlocksToBeComputed::ROBOT_LANDMARKS) override
Definition: solver_ceres.cpp:153
const Eigen::SparseMatrixd computeHessian() const
Definition: solver_ceres.cpp:849
std::map< FactorBasePtr, ceres::CostFunctionPtr > fac_2_costfunction_
Definition: solver_ceres.h:44
double finalCost() const override
Definition: solver_ceres.cpp:704
std::unique_ptr< ceres::Problem > ceres_problem_
Definition: solver_ceres.h:50
virtual int numStateBlocksEstimatedDerived() const override
Definition: solver_ceres.cpp:658
std::unique_ptr< ceres::Covariance > covariance_
Definition: solver_ceres.h:51
ceres::Solver::Summary summary_
Definition: solver_ceres.h:49
void updateStateBlockLocalParametrizationDerived(const StateBlockPtr &state_ptr) override
Definition: solver_ceres.cpp:624
std::map< FactorBasePtr, ceres::ResidualBlockId > fac_2_residual_idx_
Definition: solver_ceres.h:43
bool hasLocalParametrizationDerived(const StateBlockPtr &st) const override
Definition: solver_ceres.h:159
void addFactorDerived(const FactorBasePtr &fac_ptr) override
Definition: solver_ceres.cpp:533
void updateStateBlockStatusDerived(const StateBlockPtr &state_ptr) override
Definition: solver_ceres.cpp:613
unsigned int iterations() const override
Definition: solver_ceres.cpp:693
WOLF_SOLVER_CREATE(SolverCeres)
double totalTime() const override
Definition: solver_ceres.cpp:709
ceres::Solver::Options & getSolverOptions()
Definition: solver_ceres.h:137
void removeStateBlockDerived(const StateBlockPtr &state_ptr) override
Definition: solver_ceres.cpp:602
unsigned int n_convergence_
Definition: solver_ceres.h:55
void addStateBlockDerived(const StateBlockPtr &state_ptr) override
Definition: solver_ceres.cpp:578
std::string solveDerived(const ReportVerbosity report_level) override
Definition: solver_ceres.cpp:124
ceres::CostFunctionPtr createCostFunction(const FactorBasePtr &_fac_ptr)
Definition: solver_ceres.cpp:714
std::unique_ptr< ceres::Problem > & getCeresProblem()
Definition: solver_ceres.h:132
Solver manager for WOLF.
Definition: solver_manager.h:84
CovarianceBlocksToBeComputed
Enumeration of covariance blocks to be computed.
Definition: solver_manager.h:89
const double * getAssociatedMemBlockPtr(const StateBlockPtr &state_ptr) const
Definition: solver_manager.cpp:509
std::map< StateBlockPtr, Eigen::VectorXd > state_blocks_
Definition: solver_manager.h:206
ReportVerbosity
Enumeration for the verbosity of the solver report.
Definition: solver_manager.h:102
SparseMatrix< double > SparseMatrixd
Definition: wolf.h:112
Definition: solver_ceres.h:31
std::shared_ptr< CostFunction > CostFunctionPtr
Definition: solver_ceres.h:32
Definition: capture_base.h:23
WOLF_PTR_TYPEDEFS(CaptureLandmarksExternal)